/* 图表专用JavaScript */

// Chart.js 配置和工具函数
const ChartUtils = {
    // 默认颜色配置
    colors: {
        primary: '#4472C4',
        secondary: '#70AD47',
        warning: '#FFC000',
        danger: '#C5504B',
        info: '#5B9BD5',
        success: '#70AD47',
        light: '#F2F2F2',
        dark: '#404040'
    },

    // 获取颜色数组
    getColorArray: function(count) {
        const colors = Object.values(this.colors);
        return Array.from({length: count}, (_, i) => colors[i % colors.length]);
    },

    // 默认图表配置
    defaultOptions: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                position: 'top',
            },
            tooltip: {
                mode: 'index',
                intersect: false,
            }
        },
        scales: {
            y: {
                beginAtZero: true,
                grid: {
                    color: 'rgba(0,0,0,0.1)'
                }
            },
            x: {
                grid: {
                    color: 'rgba(0,0,0,0.1)'
                }
            }
        }
    },

    // 创建折线图
    createLineChart: function(ctx, data, options = {}) {
        const config = {
            type: 'line',
            data: data,
            options: {
                ...this.defaultOptions,
                ...options
            }
        };
        return new Chart(ctx, config);
    },

    // 创建柱状图
    createBarChart: function(ctx, data, options = {}) {
        const config = {
            type: 'bar',
            data: data,
            options: {
                ...this.defaultOptions,
                ...options
            }
        };
        return new Chart(ctx, config);
    },

    // 创建饼图
    createPieChart: function(ctx, data, options = {}) {
        const config = {
            type: 'pie',
            data: data,
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'right',
                    }
                },
                ...options
            }
        };
        return new Chart(ctx, config);
    }
};

// 仪表板图表初始化
function initDashboardCharts() {
    // 客户趋势图表
    const customerCtx = document.getElementById('customerChart');
    if (customerCtx && window.customerData) {
        ChartUtils.createLineChart(customerCtx, {
            labels: window.customerData.labels,
            datasets: [{
                label: '新增客户',
                data: window.customerData.data,
                borderColor: ChartUtils.colors.primary,
                backgroundColor: ChartUtils.colors.primary + '20',
                tension: 0.4
            }]
        });
    }

    // 商谈趋势图表
    const negotiationCtx = document.getElementById('negotiationChart');
    if (negotiationCtx && window.negotiationData) {
        ChartUtils.createLineChart(negotiationCtx, {
            labels: window.negotiationData.labels,
            datasets: [
                {
                    label: '在线商谈',
                    data: window.negotiationData.online,
                    borderColor: ChartUtils.colors.info,
                    backgroundColor: ChartUtils.colors.info + '20',
                    tension: 0.4
                },
                {
                    label: '线下商谈',
                    data: window.negotiationData.offline,
                    borderColor: ChartUtils.colors.secondary,
                    backgroundColor: ChartUtils.colors.secondary + '20',
                    tension: 0.4
                }
            ]
        });
    }
}

// 统计页面图表初始化
function initStatisticsCharts() {
    // 根据页面需要初始化不同的图表
    console.log('Statistics charts initialized');
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    initDashboardCharts();
    initStatisticsCharts();
});

